#include <stdio.h>
#include <pthread.h>
#include <string.h>
int main(int argc,char *argv[])
{
	if(argc < 3)
	{
		printf("incorrect parameter\n");
		return 1;
	}
	pthread_rwlock_t rwlock;
	pthread_rwlock_init(&rwlock,NULL);
	if(strcmp(argv[1],"r") == 0)
	{
		if(pthread_rwlock_rdlock(&rwlock) != 0)
		{
			printf("first read lock fail\n");
		}
		else
		{
			printf("first read lock success\n");
		}
	}
	else
	{
		if(pthread_rwlock_wrlock(&rwlock) != 0)
		{
			printf("first write lock fail\n");
		}
		else
		{
			printf("first write lock success\n");
		}
	}
	if(strcmp(argv[2],"r") == 0)
	{
		if(pthread_rwlock_rdlock(&rwlock) != 0)
		{
			printf("second read lock fail\n");
		}
		else
		{
			printf("second read lock success\n");
		}
	}
	else
	{
		if(pthread_rwlock_wrlock(&rwlock) != 0)
		{
			printf("second write lock fail\n");
		}
		else
		{
			printf("second write lock success\n");
		}
	}
	// 注意上面锁了两次，所以这里要解锁两次
	pthread_rwlock_unlock(&rwlock);
	pthread_rwlock_unlock(&rwlock);
	pthread_rwlock_destroy(&rwlock);
	return 0;
}
